#include <string.h>

#include "psd_backend_handler.h"

static psdBackendHandler *backend_handlers[255];

int psd_backend_handler_init() {

	bzero(backend_handlers, sizeof(psdBackendHandler *) * 255);

	return 0;
}

int psd_get_backend_handler(const char *name) {
	uint8_t i;

	for(i = 0;  i < 255; i++) {
		if (backend_handlers[i] != NULL) {
			if (strcasecmp(backend_handlers[i]->name, name) == 0)
				return i;
		}
	}

	return -1;
}

int psd_add_backend_handler(psdBackendHandler *handler) {
	uint8_t i;

	for(i = 0;  i < 255; i++) {
		if (backend_handlers[i] != NULL) {
			if (strcasecmp(backend_handlers[i]->name, handler->name) == 0)
				break;
		} else {
			break;
		}
	}

	if (i == 255) {
		return -1;
	}

	backend_handlers[i] = handler;

	return 0;
}

psdBackendHandler *psd_get_backend(const char *name) {
	int num;

	num = psd_get_backend_handler(name);
	if (num < 0) {
		goto error_exit;
	}

	return backend_handlers[num];

error_exit:
	return NULL;
}
